package com.zrwl.poscloud.base.enums;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import springfox.documentation.spring.web.json.Json;

import java.util.*;

public enum MessageModelType {
    /**
     * 短信消息模板 【sms_model】
     */
    SMS_VERIFICATION_CODE(EMessageModel.PLATFORM_TYPE_SMS,1, "短信-通用验证码","VERIFICATION_CODE",getDefaultModeParams("VERIFICATION_CODE",EMessageModel.PLATFORM_TYPE_SMS)),//通用验证码
    SMS_CARD_OPERATOR_DAILY(EMessageModel.PLATFORM_TYPE_SMS,2,"短信-卡务操作员日报","CARD_OPERATOR_DAILY",getDefaultModeParams("CARD_OPERATOR_DAILY",EMessageModel.PLATFORM_TYPE_SMS)),//卡务操作员日报
    SMS_CONSUMER_DAILY(EMessageModel.PLATFORM_TYPE_SMS,3,"短信-消费日报","CONSUMER_DAILY",getDefaultModeParams("CONSUMER_DAILY",EMessageModel.PLATFORM_TYPE_SMS)),//消费日报
    SMS_DEVICE_OFFLINE(EMessageModel.PLATFORM_TYPE_SMS,4,"短信-设备离线","DEVICE_OFFLINE",getDefaultModeParams("DEVICE_OFFLINE",EMessageModel.PLATFORM_TYPE_SMS)),//设备离线
    SMS_PURCHASED_RESOURCE_BALANCE(EMessageModel.PLATFORM_TYPE_SMS,5,"短信-已购资源余量","PURCHASED_RESOURCE_BALANCE",getDefaultModeParams("PURCHASED_RESOURCE_BALANCE",EMessageModel.PLATFORM_TYPE_SMS)),//已购资源余量

    /**
     * 微信消息模板 【wx_model】
     */
    WX_HOME_WORK_NOTICE(EMessageModel.PLATFORM_TYPE_WX,1, "微信-作业提醒","HOME_WORK",getDefaultModeParams("HOME_WORK",EMessageModel.PLATFORM_TYPE_WX)),//作业通知
    WX_LEAVE_RESULT_NOTICE(EMessageModel.PLATFORM_TYPE_WX,2, "微信-请假结果通知","LEAVE_RESULT",getDefaultModeParams("LEAVE_RESULT",EMessageModel.PLATFORM_TYPE_WX)),//请假结果通知,
    WX_OPEN_ACCOUNT_NOTICE(EMessageModel.PLATFORM_TYPE_WX,3, "微信-账户开户办理完成通知","OPEN_ACCOUNT", getDefaultModeParams("OPEN_ACCOUNT",EMessageModel.PLATFORM_TYPE_WX)),//账户开户办理完成通知
    WX_RECHARGE_NOTICE(EMessageModel.PLATFORM_TYPE_WX,4, "微信-充值通知","RECHARGE", getDefaultModeParams("RECHARGE",EMessageModel.PLATFORM_TYPE_WX)),//充值成功通知
    WX_BALANCE_REMIND(EMessageModel.PLATFORM_TYPE_WX,5, "微信-余额不足提醒","BALANCE", getDefaultModeParams("BALANCE",EMessageModel.PLATFORM_TYPE_WX)),//余额不足提醒
    WX_BUSINESS_COMPLETED_REMIND(EMessageModel.PLATFORM_TYPE_WX,6, "微信-业务办理完成提醒","BUSINESS_COMPLETED", getDefaultModeParams("BUSINESS_COMPLETED",EMessageModel.PLATFORM_TYPE_WX)),//业务办理完成提醒
    WX_CONSUME_SUCCEED_NOTICE(EMessageModel.PLATFORM_TYPE_WX,7, "微信-账户消费成功通知","CONSUME_SUCCEED", getDefaultModeParams("CONSUME_SUCCEED",EMessageModel.PLATFORM_TYPE_WX)),//账户消费成功通知
    WX_TRADE_EXCEPTION_REMIND(EMessageModel.PLATFORM_TYPE_WX,8, "微信-交易异常提醒","TRADE_EXCEPTION", getDefaultModeParams("TRADE_EXCEPTION",EMessageModel.PLATFORM_TYPE_WX)),//交易异常提醒
    WX_OPENING_CARD_NOTICE(EMessageModel.PLATFORM_TYPE_WX,9, "微信-账户开卡办理完成通知","OPENING_CARD", getDefaultModeParams("OPENING_CARD",EMessageModel.PLATFORM_TYPE_WX)),//账户开卡办理完成通知
    /**
     * 邮件消息模板 【email_model】
     */
    EMAIL_EXAMPLE_NOTICE(EMessageModel.PLATFORM_TYPE_EMAIL,1, "邮件-示例通知","EXAMPLE",getDefaultModeParams("EXAMPLE",EMessageModel.PLATFORM_TYPE_EMAIL)),//示例通知
    /**
     * 站内信消息模板 【station_model】
     */
    STATION_ANNOUNCEMENT_NOTICE(EMessageModel.PLATFORM_TYPE_STATION,1, "站内信-公告通知","ANNOUNCEMENT",getDefaultModeParams("ANNOUNCEMENT",EMessageModel.PLATFORM_TYPE_STATION)),//公告通知
    /**
     * 系统消息模板 【sys_model】
     */
    SYS_FIRST_NOTICE(EMessageModel.PLATFORM_TYPE_SYS,1, "系统消息-系统消息通知","FIRST",getDefaultModeParams("FIRST",EMessageModel.PLATFORM_TYPE_SMS)),//系统消息通知
    /**
     * 演示-End
     */
    Example(0,0, "测试", null, null);
    private final int platformType;
    private final int type;
    private final String name;
    private final String paramsKey;
    private final Map<String, String> defaultParams;


    MessageModelType(int platformType,int type, String name,String paramsKey,Map<String, String> defaultParams) {
        this.platformType = platformType;
        this.type = type;
        this.paramsKey = paramsKey;
        this.name = name;
        this.defaultParams = defaultParams;
    }
    public int getPlatformType() {
        return platformType;
    }
    public int getType() {
        return type;
    }

    public String getName() {
        return name;
    }
    public String getParamsKey() {
        return paramsKey;
    }
    public Map<String, String> getDefaultParams() {
        return defaultParams;
    }

    /**
     * 默认模板 【getDefaultModeParams】
     */
    private static @Nullable Map<String, String> getDefaultModeParams(@NotNull String name, int platformType) {
        switch (Objects.requireNonNull(EMessageModel.getLabelName(platformType))){
            case "SMS": return getSmsDefaultModeParams(name);
            case "WX":  return getWxDefaultModeParams(name);
            case "EMAIL": return null;
            case "STATION": return getStationDefaultModeParams(name);
            case "SYS": return getSysDefaultModeParams(name);
        }
        return null;
    }
    /**
     * 短信默认模板 【getSmsDefaultModeParams】
     */
    private static Map<String, String> getSmsDefaultModeParams(@NotNull String name){
        Map<String, String> resultMap = new LinkedHashMap<>();
        switch (name) {
            case "VERIFICATION_CODE"://通用验证码
            case "DEVICE_OFFLINE"://设备离线
            case "PURCHASED_RESOURCE_BALANCE"://已购资源余量
                resultMap = new LinkedHashMap<>();
                resultMap.put("1","");
                resultMap.put("2","");
                break;
            case "CARD_OPERATOR_DAILY"://卡务操作员日报
                resultMap = new LinkedHashMap<>();
                resultMap.put("1","");
                resultMap.put("2","");
                resultMap.put("3","");
                resultMap.put("4","");
                resultMap.put("5","");
                resultMap.put("6","");
                resultMap.put("7","");
                break;
            case "CONSUMER_DAILY"://消费日报
                resultMap = new LinkedHashMap<>();
                resultMap.put("1","");
                resultMap.put("2","");
                resultMap.put("3","");
                resultMap.put("4","");
                resultMap.put("5","");
                resultMap.put("6","");
                break;
        }
        return resultMap;
    }
    /**
     * 微信默认模板 【getWxDefaultModeParams】
     */
    private static Map<String, String> getWxDefaultModeParams(@NotNull String name) {
        Map<String, String> resultMap = new LinkedHashMap<>();
        switch (name){
            case "HOME_WORK"://作业通知
                resultMap = new LinkedHashMap<>();
                resultMap.put("keyword1.DATA","");
                resultMap.put("keyword2.DATA","");
                resultMap.put("keyword3.DATA","");
                break;
            case "LEAVE_RESULT"://请假结果通知
                resultMap = new LinkedHashMap<>();
                resultMap.put("phrase6.DATA", "");
                resultMap.put("short_thing1.DATA", "");
                resultMap.put("time2.DATA", "");
                resultMap.put("thing3.DATA", "");
                break;
            case "OPEN_ACCOUNT"://开户办理完成通知
            case "OPENING_CARD"://账户开卡办理完成通知
                resultMap = new LinkedHashMap<>();
                resultMap.put("thing1.DATA", "");
                resultMap.put("time2.DATA", "");
                resultMap.put("amount3.DATA", "");
                resultMap.put("amount4.DATA", "");
                break;
            case "RECHARGE"://充值通知
                resultMap = new LinkedHashMap<>();
                resultMap.put("phrase2.DATA", "");
                resultMap.put("amount3.DATA", "");
                resultMap.put("thing4.DATA", "");
                resultMap.put("time5.DATA", "");
                resultMap.put("amount6.DATA", "");
                break;
            case "BALANCE"://余额不足提醒
                resultMap = new LinkedHashMap<>();
                resultMap.put("keyword1.DATA", "");
                resultMap.put("keyword2.DATA", "");
                break;
            case "BUSINESS_COMPLETED"://业务办理完成提醒
            case "TRADE_EXCEPTION"://交易异常提醒
                resultMap = new LinkedHashMap<>();
                resultMap.put("keyword1.DATA", "");
                resultMap.put("keyword2.DATA","");
                resultMap.put("keyword3.DATA","");
                resultMap.put("keyword4.DATA","");
                break;
            case "CONSUME_SUCCEED"://账户消费成功通知
                resultMap = new LinkedHashMap<>();
                resultMap.put("thing3.DATA", "");
                resultMap.put("character_string6.DATA", "");
                resultMap.put("thing8.DATA", "");
                resultMap.put("amount4.DATA", "");
                resultMap.put("amount5.DATA", "");
                break;
        }
        return resultMap;
    }
    /**
     * 站内信默认模板 【getStationDefaultModeParams】
     */
    private static Map<String, String> getStationDefaultModeParams(@NotNull String name){
        Map<String, String> resultMap;
        switch (name) {
            case "ANNOUNCEMENT"://公告通知
                resultMap = new LinkedHashMap<>();
                resultMap.put("title","");
                resultMap.put("level","");
                resultMap.put("content","");
                resultMap.put("name","");
                resultMap.put("date","");
                break;
            default:
                return null;
        }
        return resultMap;
    }
    /**
     * 系统消息默认模板 【getSysDefaultModeParams】
     */
    private static Map<String, String> getSysDefaultModeParams(@NotNull String name){
        Map<String, String> resultMap;
        switch (name) {
            case "FIRST"://系统消息通知
                resultMap = new LinkedHashMap<>();
                resultMap.put("title","");
                resultMap.put("content","");
                break;
            default:
                return null;
        }
        return resultMap;
    }
    public static @Nullable String getParams(int platformType, MessageModelType modelType, String param) {
        Map<String, String> params = modelType.getDefaultParams();
        if(params == null){
            params = getDefaultModeParams(modelType.paramsKey,platformType);
        }
        Map<String, String> results = new HashMap<>();
        List<String> keySet = new ArrayList<>(Objects.requireNonNull(params).keySet());
        String[] valueSet = param.split(",");
        if(valueSet.length ==  keySet.size()){
            for (int i = 0; i < keySet.size(); i++) {
                String key = keySet.get(i);
                String value = valueSet[i];
                results.put(key,value);
            }
            switch (Objects.requireNonNull(EMessageModel.getLabelName(platformType))){
                case "SMS":
                    String result = "";
                    for (String value : results.values()) {
                        if(StringUtils.isEmpty(result) || StringUtils.isEmpty(result.trim())){
                            result = value;
                        }else {
                            result += result+"##"+value;
                        }
                    }
                    return result;
                case "WX":  return JSON.toJSONString(results);
                case "EMAIL": return null;
                case "STATION": return null;
                case "SYS": return null;
            }
            return null;
        }else {
            return null;
        }
    }

    public static @Nullable MessageModelType getByValue(int platformType, int type){
        for (MessageModelType modelType : values()) {
            if (modelType.getType() == type && modelType.getPlatformType() == platformType) {
                return modelType;
            }
        }
        return null;
    }
}
